package com.piece.mall.service;

import com.piece.core.framework.util.object.ObjectUtil;
import com.piece.core.jpa.repository.BaseService;
import com.piece.mall.model.PmsBrand;
import com.piece.mall.repository.PmsBrandRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class PmsBrandService extends BaseService<PmsBrand, String> {

    @Autowired
    private PmsBrandRepository pmsBrandRepository;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @PostConstruct
    public void initRepository() {
        setRepository(pmsBrandRepository);
    }

    /**
     * 获取下一个序号
     */
    public Long findNextCategorySort() {
        String sql = "select ifnull(max(brand_sort) + 1,1) from pms_brand";
        return jdbcTemplate.queryForObject(sql, Long.class);
    }

    public String findNameById(Long brandId) {
        List<String> result = jdbcTemplate.queryForList("select name from pms_brand where brand_id = ?", String.class, new Object[]{ brandId });
        if (ObjectUtil.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }
}
